package com.lzy.system.business.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzy.conf.JSONResult;
import com.lzy.system.business.dto.SysBusAddDTO;
import com.lzy.system.business.dto.SysBusUpdateDTO;
import com.lzy.system.business.dto.convert.SysBusConvert;
import com.lzy.system.business.entity.SysBus;
import com.lzy.system.business.service.ISysBusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 业务模型 前端控制器
 * </p>
 *
 * @author lcc
 * @since 2024-01-17
 */
@RestController
@RequestMapping("/business/sys-bus")
@Api(tags = "业务模型")
public class SysBusController {
    private ISysBusService iSysBusService;
    private SysBusConvert sysBusConvert;

    @Autowired
    public SysBusController(ISysBusService iSysBusService, SysBusConvert sysBusConvert) {
        this.iSysBusService = iSysBusService;
        this.sysBusConvert = sysBusConvert;
    }

    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ApiOperation("获取业务模型 列表")
    public JSONResult getList(
            @ApiParam("关键字") @RequestParam(name = "query",required = false) String query,
            @ApiParam("分组id(多个逗号间隔)") @RequestParam(name = "groups" ,required = false) String groups,
            @ApiParam("每页显示行数") @RequestParam(name = "limit", required = true, defaultValue = "10") int limit,
            @ApiParam("当前页数") @RequestParam(name = "page", required = true, defaultValue = "1") int page
    ) {
        QueryWrapper<SysBus> queryWrapper = new QueryWrapper();
        if(StrUtil.isNotBlank(query)){
            queryWrapper.and(i -> i.like("bus_name", query).or().like("bus_title", query).or().like("bus_desc", query));
        }
        if (StrUtil.isNotBlank(groups)) {
            String[] split = groups.split(",");
            queryWrapper.and(i-> {
                for (String s : split) {
                    i.eq("group_id", s);
                    i.or();
                }
            });
        }
        queryWrapper.orderByDesc("create_time");

        IPage<SysBus> page_obj = iSysBusService.page(new Page<SysBus>(page, limit), queryWrapper);
        return JSONResult.ok(page_obj);
    }


    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("添加业务模型")
    public JSONResult add(
            @ApiParam("业务模型") @RequestBody @Validated SysBusAddDTO data
    ) {
        SysBus save_data = sysBusConvert.addDtoToEntity(data);
        iSysBusService.save(save_data);
        return JSONResult.ok();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation("修改业务模型")
    public JSONResult update(
            @ApiParam("业务模型") @RequestBody @Validated SysBusUpdateDTO data
    ) {
        SysBus save_data = sysBusConvert.updateDtoToEntity(data);
        iSysBusService.updateById(save_data);
        return JSONResult.ok();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation("删除业务模型")
    public JSONResult delete(
            @ApiParam("ids") @RequestBody @Validated List<String> ids
    ) {
        iSysBusService.removeByIds(ids);
        return JSONResult.ok();
    }

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation("获取业务模型详情")
    public JSONResult get(
            @ApiParam("id") @PathVariable(value = "id") @NotBlank(message = "id不能为空") String id
    ) {
        return JSONResult.ok(iSysBusService.getById(id));
    }
}
